package com.baipiao.permission.backend.config.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.baipiao.permission.backend.entity.sys.SysPermissionEntity;
import com.baipiao.permission.backend.entity.sys.SysRoleEntity;
import com.baipiao.permission.backend.entity.sys.UserEntity;
import com.baipiao.permission.backend.service.sys.SysPermissionServiceImpl;
import com.baipiao.permission.backend.service.sys.SysRoleServiceImpl;
import com.baipiao.permission.backend.service.sys.UserServiceImpl;
import com.baipiao.permission.backend.util.ValueCheckUtil;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    UserServiceImpl UserServiceImpl;
    @Autowired
    SysRoleServiceImpl SysRoleServiceImpl;
    @Autowired
    SysPermissionServiceImpl SysPermissionServiceImpl;

    @Override
    public UserDetails loadUserByUsername(String account) {
        //获取用户
        UserEntity user = UserServiceImpl.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getAccount, account));
        if (user == null)
            throw new UsernameNotFoundException("1");
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        {//获取角色
            List<SysRoleEntity> list = SysRoleServiceImpl.list(new LambdaQueryWrapper<SysRoleEntity>()
                    .inSql(SysRoleEntity::getId, "SELECT role_id FROM sys_user_role_map map WHERE user_id=" + user.getId()));
            list.stream()
                    .filter(item -> item != null && ValueCheckUtil.valid(item.getRole()))
                    .forEach(item -> grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + item.getRole())));
        }
        {//获取权限
            List<SysPermissionEntity> list = SysPermissionServiceImpl.list(new LambdaQueryWrapper<SysPermissionEntity>()
                    .inSql(SysPermissionEntity::getId, "SELECT DISTINCT permission_id FROM sys_role_permission_map map WHERE role_id "
                            + "IN (SELECT role_id FROM sys_user_role_map map WHERE user_id=" + user.getId() + ")"));
            list.stream()
                    .filter(item -> item != null && ValueCheckUtil.valid(item.getSource()))
                    .forEach(item -> grantedAuthorities.add(new SimpleGrantedAuthority(item.getSource())));
        }
        return new User(user.getId().toString(), user.getPwd(), user.getEnable(), true,
                true, user.getUnlocked(), grantedAuthorities);
    }

}
